package com.mall.controller;



import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.entity.Goods;
import com.mall.pojo.ResponseMessage;
import com.mall.service.IGoodsService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author ShouPeng
 * @since 2024-01-14
 */
@RestController
@CrossOrigin
public class GoodsController {

    final
    IGoodsService goodsService;

    public GoodsController(IGoodsService goodsService) {
        this.goodsService = goodsService;
    }

    /**
     * 查询最新的商品
     */
    @GetMapping("/goods/new")
    public ResponseMessage queryGoodsNew(){
        return ResponseMessage.success(goodsService.queryGoodsNew());
    }

    /**
     * 查询某一个商品的详细消息
     * @param goodsId
     */
    @GetMapping("/goods")
    public ResponseMessage queryGoodsInfo(@RequestParam String goodsId){
        if (ObjectUtils.isEmpty(goodsId)){
            return ResponseMessage.failed();
        }
        return ResponseMessage.success(goodsService.queryGoodsInfo(goodsId));
    }

    /**
     * 搜索商品
     * @param pageSize 每页数据大小
     * @param page 页码
     * @param categoryId 分类ID
     * @param key 搜索关键字
     */
    @GetMapping("/goods/search")
    public ResponseMessage searchGoods(@RequestParam(defaultValue = "16") Integer pageSize,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(required = false) String categoryId,
                              @RequestParam(required = false) String key, HttpServletResponse response){
        if (ObjectUtils.isNotEmpty(categoryId)){
            return ResponseMessage.success(goodsService.queryGoodsByCategoryId(new Page<Goods>(page,pageSize),categoryId));
        }
        if (ObjectUtils.isNotEmpty(key)){
            return ResponseMessage.success(goodsService.searchGoods(new Page<Goods>(page,pageSize),key));
        }
        response.setStatus(400);
        return ResponseMessage.custom("-1","错误请求");
    }


}

